import ToiletManager from "../ToiletManager";
import BaseView from "../../../cfw/mvc/BaseView";
import ToiletController from "../ToiletController";
import WaterStreamComp from "./WaterStreamComp";
import { MAX_STATE_NUM } from "../../../public/Config";
import BagManager from "../../bag/BagManager";
import { SDKEventName } from "../../../sdk/SDKConfig";
import SDKManager from "../../../sdk/SDKManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ToiletWashView extends BaseView {

    @property(cc.Node)
    soap: cc.Node = null;


    @property(cc.Node)
    sink: cc.Node = null;


    // @property(cc.Node)
    // waterPos: cc.Node = null;

    @property([cc.Node])
    paopao: cc.Node[] = []

    @property(WaterStreamComp)
    water: WaterStreamComp = null;
    // @property(cc.SpriteFrame)
    // waterSprite: cc.SpriteFrame = null;

    protected model: ToiletManager;
    protected controller: ToiletController;

    // @property(cc.Node)
    // pops: cc.Node = null;

    // protected pool: ObjectPool<cc.Node> = new ObjectPool('water')

    private initPos: cc.Vec3 = cc.v3()

    private isClicked: boolean = false;

    protected collideCount: number = 0;


    private step: number = 0;
    onButtonHomeClick() {
        this.controller.gotoLobby()
    }
    start() {
        this.controller = ToiletController.instance();
        let pos = this.soap.position;
        this.initPos.x = pos.x;
        this.initPos.y = pos.y;
        this.model = ToiletManager.instance();
        this.controller = ToiletController.instance();
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this)
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this)
        SDKManager.getChannel().sendEvent(SDKEventName.enterwc, 1)
    }


    touchStart(e: cc.Touch) {
        if (this.step == 0) {
            let pos = e.getLocation();
            // pos = this.node.convertToWorldSpaceAR(pos)
            // cc.log(' pos ', pos.x, pos.y)
            let rect = this.soap.getBoundingBoxToWorld()
            // cc.log(' rect ', rect.x, rect.y, rect.width, rect.height)
            if (rect.contains(pos)) {
                this.isClicked = true;

            }
        }

    }

    touchMove(e: cc.Touch) {
        if (this.step == 0) {
            if (this.isClicked) {
                let pos = e.getLocation();
                pos = this.node.convertToNodeSpaceAR(pos)
                this.soap.x = pos.x;
                this.soap.y = pos.y;
                this.collide();

            }
        }

    }



    waterCollide() {
        // let finish: boolean = true;
        for (let index = 0; index < this.paopao.length; index++) {
            const element = this.paopao[index];
            // cc.log('  update  ---------------------  ')
            let parent = element.parent;
            if (parent.width > 0) {
                parent.width -= 6;
                if (parent.width <= 0) {
                    parent.width = 0;
                    this.soapNum++
                }
            } else {

            }
            if (parent.height > 0) {
                parent.height -= 6;
                if (parent.height <= 0) {
                    parent.height = 0;
                    this.soapNum++
                }
            } else {

            }
        }
        if (this.soapNum >= 4) {
            cc.log('冲洗完毕')
            this.step++;
            this.soapNum = 0;
            this.water.setStartFlag(false)
            for (let index = 0; index < this.paopao.length; index++) {
                BagManager.addCoin(10, this.paopao[index].parent.convertToWorldSpaceAR(this.paopao[index].getPosition()))
            }

            cc.tween(this).to(1, {}).call(() => {
                this.model.addNum(MAX_STATE_NUM / 4)
                this.model.updateExp()
                this.controller.gotoLobby();
            }).start();

        }
    }
    private soapNum: number = 0;
    collide() {
        let pos = this.soap.getPosition();
        pos = this.soap.parent.convertToWorldSpaceAR(pos)
        // let finish: number = 0;
        // cc.log(' pos ', pos.x, pos.y)
        for (let index = 0; index < this.paopao.length; index++) {
            const element = this.paopao[index];
            let rect = element.getBoundingBoxToWorld();
            // cc.log(' rect ', rect.x, rect.y, rect.width, rect.height)
            if (rect.contains(pos)) {
                // cc.log('  update  ---------------------  ')
                let parent = element.parent;
                if (parent.width < 256) {
                    parent.width += 2;
                    if (parent.width >= 256) {
                        this.soapNum++
                    }
                }
                if (parent.height < 200) {
                    parent.height += 2;
                    if (parent.height >= 200) {
                        this.soapNum++
                    }
                }
            }
        }

        if (this.soapNum >= 4) {
            cc.log('香皂上好')
            this.touchEnd();
            this.step = 1
            this.soapNum = 0
            this.water.setStartFlag(true)

            // this.startWater();
        }
    }

    touchEnd() {
        if (this.step == 0) {
            if (this.isClicked) {
                this.isClicked = false;
                this.soap.x = this.initPos.x
                this.soap.y = this.initPos.y;
            }
        }

    }

    // update (dt) {}
}
